/**
 * 全局配置
 */
const path = require('path');
const jsonfile = require('jsonfile');

let base_dir = path.join(__dirname, '..');
if (process.env.NODE_ENV === 'production') {
    // base_dir = path.join(__dirname, '..', '..');
}

// 相关配置
let config;
const configFile = path.join(__dirname) + '/config.json';
if (require('fs').existsSync(configFile)) {
    config = jsonfile.readFileSync(configFile);
} else {
    // 首次生成默认的基本配置
    initConfig('');
}

function initConfig(ip) {
    let localIP = ip;
    if (!ip) {
       localIP = getIPAdress();
    }
    config = {
        "web_host": {
            "ip": localIP,
            "port": 8088
        },
        "res_host": {
            "ip": localIP,
            "port": 8088
        },
        "server_host": {
            "port": 8098
        },
        "redis_host": {
            "ip": localIP,
            "port": 6379
        },
        "ipc_host": {
            "ip": "127.0.0.1",
            "port": 7008
        },
        "recog_host": {
            "ip": "127.0.0.1",
            "port": 8078
        },
        "mongodb_host": {
            "ip": "127.0.0.1",
            "port": 22002
        },
        "liveness_host": {
            "ip": "127.0.0.1",
            "port": 3005
        },
    };
    jsonfile.writeFileSync(configFile, config);
}

function getIPAdress() {
    const interfaces = require('os').networkInterfaces();
    for (let devName in interfaces) {
        let iface = interfaces[devName];
        for (let i = 0; i < iface.length; i++) {
            let alias = iface[i];
            if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
                return alias.address;
            }
        }
    }
}


function setConfig(cfg) {
    config = cfg;
    jsonfile.writeFileSync(configFile, config);
}

module.exports = {
    initConfig: initConfig,
    setConfig: setConfig,
    config: config,
    log_dir: base_dir + '/eg_server_logs/', // 日志路径
    port: 8098,
    port_https: 8099,
    public_cors_origin:`http://116.196.89.55:8088`, //公网部署下的cors跨域信任URL
    cors_origin: `http://${config.web_host.ip}:${config.web_host.port}`, // cors跨域信任URL
    liveness: {
        detect: `http://${config.liveness_host.ip}:${config.liveness_host.port}/liveness`,
    },
    res: {
        base_pic_uri: `http://${config.res_host.ip}:${config.res_host.port}/pic/`, // 显示头像url前缀
        base_record_uri: `http://${config.res_host.ip}:${config.res_host.port}/record/`, // 抓拍头像url前缀
        avatar_dir: base_dir + '/res/avatar/pic/', // 显示头像路径
        capture_dir: base_dir + '/res/capture/', // 抓拍头像zip包路径
        uploader_dir: base_dir + '/res/uploader/', // 批量上传临时路径
        downloadlog_dir: base_dir + '/res/log/', // 批量上传临时路径
        stranger_face_dir: base_dir + '/res/avatar/record/', // 抓拍人脸路径

        // 巨龙抓拍机
        julong_record_dir: base_dir+ '/res/julong_record/',
        julong_record_dirname: '/julong_record/'
        // base_julong_record_uri: `http://${config.res_host.ip}:${config.res_host.port}/julong_record/`, // 巨龙抓拍照片url前缀
    },
    ipc: {
        update: `http://${config.ipc_host.ip}:${config.ipc_host.port}/update`,
        delete: `http://${config.ipc_host.ip}:${config.ipc_host.port}/delete`,
        status: `http://${config.ipc_host.ip}:${config.ipc_host.port}/status`,
        start: `http://${config.ipc_host.ip}:${config.ipc_host.port}/start`,
        stop: `http://${config.ipc_host.ip}:${config.ipc_host.port}/stop`,
        restart: `http://${config.ipc_host.ip}:${config.ipc_host.port}/restart`
    },
    feature: {
        model: 'f1214', // 特征模型
        register: `http://${config.recog_host.ip}:${config.recog_host.port}/feature_regist`,
        delete: `http://${config.recog_host.ip}:${config.recog_host.port}/features_delete`,
        recognize: `http://${config.recog_host.ip}:${config.recog_host.port}/feature_face`,
        record: `http://${config.recog_host.ip}:${config.recog_host.port}/record`
    },
    mongodb: {
        host: config.mongodb_host.ip,
        port: config.mongodb_host.port,
        database: 'gate_guard',
        user: "dev",
        pass: "Aa123456"
    },
    redis: {
        host: config.redis_host.ip,
        port: config.redis_host.port,
        password: 'testredis'
    },
    account: {
        id: "admin",
        pwd: "e10adc3949ba59abbe56e057f20f883e"
    },
    // 人脸识别服务
    faceRecognitionServer: 'http://116.196.123.13:3012',
    // 佳一客户服务器
    jiayiServer:'http://localhost:9277'
}
